package com.tbsoft.ehr.service.common;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.tbsoft.ehr.dao.common.UploadFileDao;
import com.tbsoft.ehr.model.AttachFile;
import com.tbsoft.ehr.util.ConfigProps;
import com.tbsoft.ehr.util.FileUtils;

/**
 * @desc 附件上传实现
 * @author vastdream
 *
 */
public class UploadFileServiceImpl implements UploadFileService {
	private String tmpAttachPath = (String) ConfigProps.getContextProperty("attach.file.path")+File.separator+"tmp";
	
	private UploadFileDao uploadFileDao;

	public synchronized void setUploadFileDao(UploadFileDao uploadFileDao) {
		this.uploadFileDao = uploadFileDao;
	}
	@Override
	public String receiveFile(String upLoadFileName, String receiveFileName) throws IOException {
		File tmpFilePath = new File(tmpAttachPath);
		if(!tmpFilePath.exists()||!tmpFilePath.isDirectory()){
			tmpFilePath.mkdirs();
		}
		String tmpFileName = String.valueOf(UUID.randomUUID());
		FileUtils.copyFile(new File(receiveFileName), new File(tmpAttachPath+File.separator+tmpFileName));
		return tmpFileName;
	}
	
	 
	 @Override
	public List<AttachFile> getAttachListByBusinessId(String businessId) {
		return uploadFileDao.getAttachListByBusinessId(businessId);
	}

}
